package com.xdtech.trans.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xdtech.common.fomat.TreeBuilder;
import com.xdtech.sys.aspect.SystemControllerLog;
import com.xdtech.trans.model.TransWebSite;
import com.xdtech.trans.service.TransWebSiteService;
import com.xdtech.trans.vo.TransWebSiteItem;
import com.xdtech.web.model.ComboBox;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;
import com.xdtech.web.model.TreeItem;

/**
 * 
 * @author max.zheng
 * @create 2015-05-26 21:12:50
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
public class TransWebSiteController {
	private Log log = LogFactory.getLog(TransWebSiteController.class);
	@Autowired
	private TransWebSiteService transWebSiteService;
	@RequestMapping(value="/transWebSite.do",params = "transWebSite")
	public ModelAndView skipTransWebSite() {
		return new ModelAndView("trans/transWebSite/transWebSite_ftl");
	}
	
	@RequestMapping(value="/transWebSite.do",params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(TransWebSiteItem item,Pagination pg) {
		Map<String, Object> results =  transWebSiteService.loadPageDataByConditions(pg,item,"findTransWebSiteByCondition");
		return results;
	}
	
	@RequestMapping(value="/transWebSite.do",params = "editTransWebSite")
	@SystemControllerLog(description = "编辑")
	public ModelAndView editTransWebSite(HttpServletRequest request,Long transWebSiteId) {
		if (transWebSiteId!=null) {
			request.setAttribute("transWebSiteItem", transWebSiteService.loadTransWebSiteItem(transWebSiteId));
		}
		return new ModelAndView("trans/transWebSite/editTransWebSite_ftl");
	}
	
	@RequestMapping(value="/transWebSite.do",params = "saveTransWebSite")
	@ResponseBody
	@SystemControllerLog(description = "保存")
	public ResultMessage saveTransWebSite(TransWebSiteItem item) {
		ResultMessage r = new ResultMessage();
		if ((item.getParentId()+"").equals(item.getId()+"")) {
			r.setSuccess(false);
			r.setMsg("上级网点不能是自己.");
		}else {
			if (transWebSiteService.saveOrUpdateTransWebSite(item)) {
				r.setSuccess(true);
			}else {
				r.setSuccess(false);
			}
		}
		return r;
	}
	
	@RequestMapping(value="/transWebSite.do",params = "deleteTransWebSiteItems")
	@ResponseBody
	@SystemControllerLog(description = "删除")
	public ResultMessage deleteTransWebSiteItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> transWebSiteIds = new ArrayList<Long>();
			for (String id : tempIds) {
				transWebSiteIds.add(Long.valueOf(id));
			}
			transWebSiteService.deleteTransWebSiteInfo(transWebSiteIds);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/transWebSite.do",params = "websiteTree")
	@ResponseBody
	public List<TreeItem> websiteTree(HttpServletRequest request,String websiteId) {
		List<TreeItem> items = new ArrayList<TreeItem>();
//		Iterable<UserGroup> usergroups = userGroupService.loadUserGroups();
//		List<TransWebSiteItem> webSiteItems = transWebSiteService.loadItems();
		List<TransWebSite> webSiteItems = transWebSiteService.getAll();
		TreeItem item = null;
		TreeItem pItem = null;
		for (TransWebSite transWebSiteItem : webSiteItems)
		{
			item = new TreeItem();
			item.setId(transWebSiteItem.getId());
			item.setText(transWebSiteItem.getName());
			item.setContent(transWebSiteItem.getSimpleName());
			if (transWebSiteItem.getParent()!=null) {
				pItem = new TreeItem(transWebSiteItem.getParent().getId());
			}else {
				pItem = null;
			}
			if (StringUtils.isNotBlank(websiteId)&&item.getId().toString().equals(websiteId))
			{
				//已有用户组设置选中状态
				item.setChecked(true);
			}
			item.setParent(pItem);
			items.add(item);
		}
		return TreeBuilder.newTreeBuilder(TreeItem.class, Long.class).buildToTreeList(items);
	}
	
	
	@RequestMapping(value="/transWebSite.do",params = "loadTransWebSiteInfo")
	@ResponseBody
	public TransWebSiteItem loadTransWebSiteInfo(Long id) {
		TransWebSiteItem item = transWebSiteService.loadTransWebSiteItem(id);
		return item;
	}
	
	@RequestMapping(value="/transWebSite.do",params = "loadWebsiteCombox")
	@ResponseBody
	public List<ComboBox> loadWebsiteCombox(String value,String text) {
		List<TransWebSite> transWebSites = transWebSiteService.getAll();
		List<ComboBox> comboBoxs = new ArrayList<ComboBox>();
		for (TransWebSite wb : transWebSites)
		{
			comboBoxs.add(new ComboBox(wb.getId().toString(), wb.getName(), wb.getId().toString().equals(value)||wb.getName().equals(text)));
		}
		return comboBoxs;
	}
}
